package com.dd.cloud.user.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.user.entity.educationvisit.PatientEducationVisit;
import com.dd.cloud.user.entity.educationvisit.PatientEducationVisitPushRecord;
import com.dd.cloud.user.entity.educationvisit.PatientEducationVisitTag;
import com.dd.cloud.user.req.education.*;
import com.dd.cloud.user.res.education.PatientEducationOrderRes;
import com.dd.cloud.user.res.education.PatientEducationRecordOrderRes;
import com.dd.cloud.user.res.education.PatientEducationRecordRes;
import com.dd.cloud.user.res.education.PatientEducationUseDrugRes;
import com.dd.cloud.user.service.educationvisit.IPatientEducationPushRecordService;
import com.dd.cloud.user.service.educationvisit.IPatientEducationVisitService;
import com.dd.cloud.user.service.educationvisit.IPatientEducationVisitTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 患教随访管理 前端控制器
 * </p>
 *
 * @author czg
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/education/visit")
public class PatientEducationVisitController {

    @Autowired
    private IPatientEducationVisitService patientEducationArticleService;


    @Autowired
    private IPatientEducationVisitService patientEducationVisitService;

    @Autowired
    private IPatientEducationPushRecordService patientEducationPushRecordService;


    @Autowired
    private IPatientEducationVisitTagService patientEducationVisitTagService;


    /**
     * 查询患教文章(分页)
     *
     * @param req
     */
    @GetMapping(value = "/page")
    public ResponseEntity<IPage<PatientEducationVisit>> queryPatientEducationArticlePage(@Valid PatientEducationArticleReq req) {
        Page<PatientEducationVisit> page = patientEducationArticleService.queryPatientEducationArticlePage(req);
        return ResponseEntity.ok(page);
    }


    /**
     * 添加患教文章
     *
     * @param from
     */
    @PostMapping(value = "/article/add")
    public ResponseEntity<Void> insertPatientEducationArticle(@Valid @RequestBody PatientEducationArticleAddFrom from) {
        patientEducationArticleService.insertPatientEducationArticle(from);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改患教文章
     *
     * @param from
     */
    @PostMapping(value = "/article/update")
    public ResponseEntity<Void> updatePatientEducationArticle(@Valid @RequestBody PatientEducationArticleUpdateFrom from) {
        patientEducationArticleService.updatePatientEducationArticle(from);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询患教/随访详情
     *
     * @param articleId 患教/随访id
     */
    @GetMapping("/detail")
    public ResponseEntity<PatientEducationVisit> queryPatientEducationArticleDetailById(@RequestParam("articleId") Integer articleId) {
        PatientEducationVisit patientEducationArticle = patientEducationArticleService.queryPatientEducationArticleDetailById(articleId);
        return ResponseEntity.ok(patientEducationArticle);
    }


    /**
     * 修改状态
     *
     * @param articleId 患教/随访id
     * @param status    -1.删除 3.禁用
     */
    @PostMapping("/update/status")
    public ResponseEntity<Void> updatePatientEducationArticleStatus(@NotNull(message = "文章id不能为空") Integer articleId, @NotNull(message = "状态不能为空") Integer status) {
        patientEducationArticleService.updatePatientEducationArticleStatus(articleId, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加患教阅读数
     *
     * @param articleId
     */
    @PostMapping("/read/count")
    public ResponseEntity<Void> updatePatientEducationArticleReadCount(@NotNull(message = "文章id不能为空") Integer articleId) {
        patientEducationArticleService.updatePatientEducationArticleReadCount(articleId);
        return ResponseEntity.ok().build();
    }


    /**
     * 查询患教文章记录
     *
     * @param req
     */
    @GetMapping("/record/page")
    public ResponseEntity<Page<PatientEducationRecordRes>> queryPatientEducationPushRecordPage(@Valid PatientEducationPushRecordReq req) {
        Page<PatientEducationRecordRes> page = patientEducationArticleService.queryPatientEducationPushRecordPage(req);
        return ResponseEntity.ok(page);
    }

    /**
     * 重新推送
     *
     * @param id
     */
    @PostMapping("/again/push/template")
    public ResponseEntity<Void> pushPatientEducationPushRecord(@NotNull(message = "推送id不能为空") Integer id) {
        patientEducationArticleService.pushPatientEducationPushRecord(id);
        return ResponseEntity.ok().build();
    }


    /**
     * 查询用药记录
     *
     * @param req
     */
    @GetMapping("/user/drug/record/page")
    public ResponseEntity<Page<PatientEducationUseDrugRes>> queryUserDrugRecordPage(@Valid PatientEducationVisitReq req) {
        Page<PatientEducationUseDrugRes> page = patientEducationVisitService.queryUserDrugRecordPage(req);
        return ResponseEntity.ok(page);
    }

    /**
     * 填写问卷
     *
     * @param id
     * @param content
     */
    @PostMapping("/write/questionnaire")
    public ResponseEntity<Void> writeQuestionnaire(@NotBlank(message = "内容不能为空") String content,
                                                   @NotNull(message = "问卷id不能为空") Integer id) {
        patientEducationVisitService.writeQuestionnaire(content, id);
        return ResponseEntity.ok().build();
    }


    /**
     * 提交随访感受
     *
     * @param id
     * @param feel
     * @param reply
     */
    @PostMapping("/feedback/submit")
    public ResponseEntity<Void> writeQuestionnaireFeel(String reply, @NotNull(message = "随访id不能为空") Integer id, @NotBlank(message = "就诊感受不能为空") String feel) {
        patientEducationVisitService.writeQuestionnaireFeel(feel, reply, id);
        return ResponseEntity.ok().build();
    }


    /**
     * 是否提交随访内容
     *
     * @param id
     */
    @GetMapping("/feedback/detail")
    public ResponseEntity<Boolean> queryIsSubmitFeedBack(@RequestParam("id") Integer id) {
        Boolean flag = patientEducationVisitService.queryIsSubmitFeedBack(id);
        return ResponseEntity.ok(flag);
    }


    /**
     * 查询随访/患教记录详情
     *
     * @param id
     */
    @GetMapping("/record/detail")
    public ResponseEntity<PatientEducationVisitPushRecord> queryPatientEducationVisitPushRecordDetail(@RequestParam("id") Integer id) {
        PatientEducationVisitPushRecord patientEducationVisitPushRecord = patientEducationVisitService.queryPatientEducationVisitPushRecordDetail(id);
        return ResponseEntity.ok(patientEducationVisitPushRecord);
    }

    /**
     * 查询云诊室患教/随访文章
     *
     * @param req
     */
    @GetMapping("/cloud/consulting/room/page")
    public ResponseEntity<Page<PatientEducationRecordRes>> queryCloudConsultingRoomPage(@Valid PatientEducationPushRecordReq req) {
        Page<PatientEducationRecordRes> page = patientEducationArticleService.queryCloudConsultingRoomPage(req);
        return ResponseEntity.ok(page);
    }

    /**
     * 推送随访/患教给患者
     *
     * @param id
     * @param type
     * @param medId
     * @param orderNo
     */
    @PostMapping("/push/template")
    public ResponseEntity<Void> pushTemplateToPatient(@NotNull(message = "患教/随访id不能为空") Integer id,
                                                      @NotNull(message = "发送类型不能为空") Integer type,
                                                      @NotNull(message = "患者id不能为空") String medId,
                                                      @NotBlank(message = "订单号") String orderNo) {
        patientEducationVisitService.pushTemplateToPatient(id, type, medId, orderNo);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新阅读次数
     */
    @PostMapping("/update/num")
    public ResponseEntity<Void> updateEducationNum(@NotNull(message = "推送id不能空") Integer pushId) {
        patientEducationPushRecordService.updateEducationNum(pushId);
        return ResponseEntity.ok().build();
    }

    /**
     * 订单详情模块 查询患教文章/问卷(分页)
     *
     * @param req
     * @return
     */
    @GetMapping("/order/page")
    public ResponseEntity<IPage<PatientEducationOrderRes>> queryPatientEducationOrder(@Valid PatientEducationOrderReq req) {
        IPage<PatientEducationOrderRes> res = patientEducationArticleService.queryPatientEducationOrder(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 创建电话随访记录
     *
     * @param req
     */
    @PostMapping("/record/dh/create")
    public ResponseEntity<Void> createPatientEducationVisitRecordSystem(@Valid @RequestBody PatientEducationVisitRecordSystemCreateReq req) {
        patientEducationArticleService.createPatientEducationVisitRecordSystem(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 订单模块 查询患教文章/随访/电话随访/用药提醒记录
     *
     * @param req
     * @return
     */
    @GetMapping("/record/order/page")
    public ResponseEntity<IPage<PatientEducationRecordOrderRes>> queryPatientEducationRecordOrder(@Valid PatientEducationRecordOrderReq req) {
        IPage<PatientEducationRecordOrderRes> res = patientEducationArticleService.queryPatientEducationRecordOrder(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 重新推送用药提醒
     *
     * @param id      推送用药提醒记录id
     * @param medId   就诊卡号
     * @param tencode 处方编号
     */
    @PostMapping("/again/push")
    public ResponseEntity<Void> againPushDrugTemplateToPatient(@NotNull(message = "推送用药提醒记录id不能为空") Integer id,
                                                               @NotBlank(message = "就诊卡号不能为空") String medId,
                                                               @NotBlank(message = "处方编号不能为空") String tencode) {
        patientEducationArticleService.againPushDrugTemplateToPatient(id, medId, tencode);
        return ResponseEntity.ok().build();
    }


    /**
     * 添加标签
     *
     * @param type 1.患教 2.随访
     * @param name 标签名
     */
    @PostMapping("/add/tag")
    public ResponseEntity<Void> addPatientEducationTag(@NotBlank(message = "标签名不能为空") String name,
                                                       @NotNull(message = "类型不能为空") Integer type) {
        patientEducationVisitTagService.addPatientEducationTag(name, type);
        return ResponseEntity.ok().build();
    }


    /**
     * 编辑编辑
     *
     * @param name  标签名
     * @param tagId 标签id
     */
    @PostMapping("/update/tag")
    public ResponseEntity<Void> updatePatientEducationTag(@NotNull(message = "标签id不能为空") Integer tagId,
                                                          @NotBlank(message = "修改名字不能为空") String name) {
        patientEducationVisitTagService.updatePatientEducationTag(tagId, name);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改状态
     *
     * @param tagId 标签id
     */
    @PostMapping("/update/tag/status")
    public ResponseEntity<Void> updateTagStatus(@NotNull(message = "标签id不能为空") Integer tagId) {
        patientEducationVisitTagService.updateTagStatus(tagId);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询列表
     *
     * @param type 1.患教 2.随访
     */
    @GetMapping("/tag/list")
    public ResponseEntity<List<PatientEducationVisitTag>> queryTagList(@RequestParam("type") Integer type) {
        List<PatientEducationVisitTag> list = patientEducationVisitTagService.queryTagList(type);
        return ResponseEntity.ok(list);
    }
}

